package com.mcxx.modules.filemanage.file.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.util.FailureCode;
import com.mcxx.util.UnifiedStorage;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.yhdcs.client.cloudmodel.OSObject;
import com.yinhai.yhdcs.core.common.DcsServerException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class FileServiceImpl extends BaseService implements FileService {

    private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Autowired
    private UnifiedStorage unifiedStorage;


    @Override
    public String upload(InputStream inputStream, String fileName) {
        String objectId = null;
        try {
            objectId = unifiedStorage.uploadFile(inputStream, fileName);
        } catch (DcsServerException e) {
            logger.error("上传文件失败：{}", e);
            throw new BizException(FailureCode.ERR_20078);
        }
        return objectId;
    }


    @Override
    public byte[] download(String objectId) {
        return this.download(objectId, null);
    }


    @Override
    public byte[] download(String objectId, String bucketId) {
        logger.info("正在下载文件");
        OSObject osObject = null;
        try {
            osObject = unifiedStorage.downloadFile(objectId, bucketId);
        } catch (DcsServerException e) {
            throw new BizException("-1",e.getMessage());
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        InputStream input = osObject.getContent();
        byte[] buffer = new byte[4096];
        try {
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new BizException(FailureCode.ERR_9999);
        }


        return output.toByteArray();
    }


    @Override
    public byte[] downloadCheckFile(String objectId) {
        logger.info("正在下载文件");
        OSObject osObject = null;
        try {
            osObject = unifiedStorage.download(objectId);
        } catch (DcsServerException e) {
            throw new BizException("-1",e.getMessage());
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        InputStream input = osObject.getContent();
        byte[] buffer = new byte[4096];
        try {
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new BizException(FailureCode.ERR_9999);
        }


        return output.toByteArray();
    }


    @Override
    public String getFileUrl(String objectId) {
        return this.getFileUrl(objectId, null);
    }


    @Override
    public String getFileUrl(String objectId, String bucketId) {
        return unifiedStorage.getFileUrl(objectId, bucketId);
    }



    @Override
    public void deleteByObjectId(String objectId) {
        unifiedStorage.deleteFile(objectId);
    }


    @Override
    public void batchDeleteByObjectId(List<String> objectIds) {
        unifiedStorage.deleteFile(objectIds);
    }
}
